package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.danciyixue.boot.dcyx.model.vo.UserCurrentProgramVO;
import com.danciyixue.boot.dcyx.model.vo.CurrentProgramVO;
import com.danciyixue.boot.dcyx.service.UserCurrentProgramService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.core.security.util.SecurityUtils;
import com.danciyixue.boot.dcyx.converter.ProgramsConverter;
import com.danciyixue.boot.dcyx.mapper.ProgramsMapper;
import com.danciyixue.boot.dcyx.service.ProgramsService;
import com.danciyixue.boot.dcyx.model.entity.Programs;
import com.danciyixue.boot.dcyx.model.entity.UserCurrentProgram;
import com.danciyixue.boot.dcyx.model.form.ProgramsForm;
import com.danciyixue.boot.dcyx.model.query.ProgramsQuery;
import com.danciyixue.boot.dcyx.model.vo.ProgramsVO;
import com.danciyixue.boot.dcyx.converter.ProgramsConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 教材信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:24
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class ProgramsServiceImpl extends ServiceImpl<ProgramsMapper, Programs> implements ProgramsService {

    private final ProgramsConverter programsConverter;
    private final UserCurrentProgramService userCurrentProgramService;

    /**
     * 获取教材信息分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage< ProgramsVO >} 教材信息分页列表
     */
    @Override
    public IPage<ProgramsVO> getProgramsPage(ProgramsQuery queryParams) {
        Page<ProgramsVO> pageVO = this.baseMapper.getProgramsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取教材信息表单数据
     *
     * @param id 教材信息ID
     * @return 教材信息表单数据
     */
    @Override
    public ProgramsForm getProgramsFormData(Long id) {
        Programs entity = this.getById(id);
        return programsConverter.toForm(entity);
    }
    
    /**
     * 新增教材信息
     *
     * @param formData 教材信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean savePrograms(ProgramsForm formData) {
        Programs entity = programsConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新教材信息
     *
     * @param id   教材信息ID
     * @param formData 教材信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updatePrograms(Long id,ProgramsForm formData) {
        Programs entity = programsConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除教材信息
     *
     * @param ids 教材信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deletePrograms(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的教材信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    /**
     * 根据当前用户Token获取当前教材
     *
     * @return 当前教材信息
     */
    @Override
    public ProgramsVO getCurrentProgramByUserToken() {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 通过用户ID获取当前选中的教材项
        UserCurrentProgramVO userCurrentProgram = userCurrentProgramService.getCurrentProgramByUserId(userId);

        if (userCurrentProgram == null || userCurrentProgram.getProgramId() == null) {
            return null;
        }

        // 获取教材详细信息
        Programs program = this.getById(userCurrentProgram.getProgramId());
        if (program == null) {
            return null;
        }

        // 转换为VO对象并返回
        ProgramsVO programsVO = programsConverter.toVO(program);

        // 可以在这里添加额外的信息，例如系列信息等

        return programsVO;
    }


}
